package com.boost.bio.line;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class MyInputStream {
    private final InputStream inputStream;
    private boolean zip;
    private ZipFile zf;
    private ZipInputStream zin;

    public MyInputStream(String filePath) throws IOException {
        if (filePath.endsWith(".zip")) {
            zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(filePath)), Charset.forName("utf-8"));
            zf = new ZipFile(filePath,Charset.forName("utf-8"));
            inputStream = zf.getInputStream(zin.getNextEntry());
            zip = true;
        }else{
            inputStream = new FileInputStream(filePath);
        }
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void close() throws IOException {
        if(zip){
            zin.closeEntry();
            zin.close();
            zf.close();
        }
    }
}
